
const config = require('../config');
const attestationApi = config.attestationApi.url;
const axios = require('axios');
const { IndexedTx,SigningStargateClient, StargateClient } = require("@cosmjs/stargate")
const { MsgDepositForBurn, MsgSendMessage } = require("../generated/circle/cctp/v1/tx")
const Web3 = require('web3');
const web3 = new Web3();


let txHash = "30C8B5A9B9C73258B404CECA3AD6C3419EA03F1112DD1CF9FADEA4B3AFFBA105"

const run = async ()=>{

    const rpc = config.noble.url;

    const client = await StargateClient.connect(rpc)
    let tx = await client.getTx(txHash)
    //console.log('tx: ',tx);
    let events = tx.events;
    let message;
    for(let event of events) {
        if(event.type === "circle.cctp.v1.MessageSent"){
            message = event.attributes;
            break;
        }
    }
    let m1 = message[0].value;
    console.log('m1: ',m1);

    let tempMessage = m1.substr(1,m1.length-2);
    console.log("tempMessage: ", tempMessage)
    var b = Buffer.from(tempMessage, 'base64')
    console.log('b: ', b.toString('hex'));
    const messageHash = web3.utils.keccak256(b);
    console.log('messageHash: ', messageHash);
    const circleUrl = attestationApi + messageHash;
    console.log('circleUrl:', circleUrl)
    //const res = await axios.get(circleUrl);
    //console.log('res.data', res.data);
}

run();

/*
message:
"00000000000000040000000000000000000001a600000000000000000000000057d4eaf1091577a6b7d121202afbd2808134f117000000000000000000000000d0c3da
58f55358142b8d3e06c1c30c5c6114efe8000000000000000000000000ef48c3ab7f184e469504bdbd9c07883ab4427c6600000000487039debedbf32d260137b0a6f66b90
962bec777250910d253781de326a716d000000000000000000000000dc484b57a5cc0563b42c2e3d37466b14186f501b000000000000000000000000000000000000000000
0000000000000000003039000000000000000000000000e97e4b1538f1401f1772779227edfa02e466e37f"

* https://iris-api-sandbox.circle.com/attestations/0xcc3e0559ab976646206f2bd505b0767fa5db204f9ef7677b0fcb93e5d77012f1
*
* {"attestation":"0x927d746d3ad52ef089b6864d05640be0029c7ddcfe77392cd5963b9b645fe3ec59dbe7dbb223e125c1f2b5fe5f44e1eca62fd0a8780f01f2f8389357b42f43c61c98daf1a5caf7d3dea5e9aaedd973da23668812238f984089cf333f93841b667f37891dc23ae7ec1c67c50e06064b81feb80fe0a388e71bd697a206119da5a7141c","status":"complete"}
* */